import { OperationDynamic } from '@/components/Generate/subComps/OperationDynamic';
import { renderColumn } from '@/components/ProTableDynamic/config';
import { ProTableContexteValue } from '@/components/ProTableDynamic/utils/types';
import lodash from 'lodash';
import { replaceStringByRegExp } from './regexp';
const operationTag = 'operationTag';

export default (initDataParam: any, columnsStr: string) => {
  let initData = JSON.parse(JSON.stringify(initDataParam));
  let columns = [];
  columns = initData.tableConfig.columns?.map((item: any) => {
    // columns中无法被序列化的数据
    if (item.dataIndex === 'table-operation') {
      return { ...item, render: operationTag };
    }
    return item;
  });
  initData.tableConfig.columns = columns;

  let initDataStr = JSON.stringify(initData);
  initDataStr = replaceStringByRegExp(operationTag, initDataStr, columnsStr); // 替换columns中无法序列化的内容
  return initDataStr;
};

export const generateDealString = (key: string): string => {
  switch (key) {
    case 'tableOperation':
      return (
        '///去除引号(_: React.ReactNode, record: any, index: number) => {' +
        '    return OperationDynamic(["detail", "edit", "delete"], onEvent, { reactNode: _, record, index });' +
        '  },///去除引号'
      );
    default:
      return '';
  }
};

export const restoreFromLocalStorage = (proTableContexteValue: ProTableContexteValue, onSettingEvent: any): ProTableContexteValue => {
  let newProTableContexteValue = lodash.cloneDeep(proTableContexteValue);
  newProTableContexteValue.tableInfo.tableConfig.columns = newProTableContexteValue.tableInfo.tableConfig.columns.map((item: any) => {
    // columns中无法被序列化的数据
    if (item.dataIndex === 'table-operation') {
      return { ...item, render: renderColumn(onSettingEvent) };
    }
    return item;
  });
  return newProTableContexteValue;
};
